package tech.wetech.admin.modules.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tech.wetech.admin.core.utils.Result;
import tech.wetech.admin.modules.base.service.impl.BaseService;
import tech.wetech.admin.modules.system.mapper.CompanyMapper;
import tech.wetech.admin.modules.system.mapper.PayCompanyInfoMapper;
import tech.wetech.admin.modules.system.po.AliPayInfo;
import tech.wetech.admin.modules.system.po.Company;
import tech.wetech.admin.modules.system.po.PayCompanyInfo;
import tech.wetech.admin.modules.system.service.PostCompanyInfoService;
import tech.wetech.admin.modules.system.vo.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PostCompanyInfoServiceImpl extends BaseService<PayCompanyInfo> implements PostCompanyInfoService {

    @Autowired
    private PayCompanyInfoMapper payCompanyInfoMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Result createUnfiedpayCompanyInfo(PayCompanyInfo payCompanyInfo) {

        //是否需要配置设备的支付json
        Boolean boolIsEverySet= Boolean.valueOf(companyMapper.getIsEverySet(payCompanyInfo.getCompanyId()));

        if (!boolIsEverySet){
            byte payChannel;
            String payChannelCompany=null;
            String json="";
            if (payCompanyInfo.getWxPayJsonVo()!=null){
                payChannel=1;
                payCompanyInfo.setPayChannel(payChannel);
                json = JSON.toJSONString(payCompanyInfo.getWxPayJsonVo());
                String strPayChannelCompany=companyMapper.payChannel(payCompanyInfo.getCompanyId());
                StringBuffer buffer = new StringBuffer(strPayChannelCompany);
                buffer.replace(0, 1, "1");
                payChannelCompany=buffer.toString();
            }else if (payCompanyInfo.getZfbPayJsonVo()!=null){
                payChannel=2;
                payCompanyInfo.setPayChannel(payChannel);

                json = JSON.toJSONString(payCompanyInfo.getZfbPayJsonVo());
                String strPayChannelCompany=companyMapper.payChannel(payCompanyInfo.getCompanyId());
                StringBuffer buffer = new StringBuffer(strPayChannelCompany);
                buffer.replace(1, 2, "1");
                payChannelCompany=buffer.toString();
            }else if (payCompanyInfo.getUnionJsonPayVo()!=null){
                payChannel=4;
                payCompanyInfo.setPayChannel(payChannel);

                json = JSON.toJSONString(payCompanyInfo.getUnionJsonPayVo());
                String strPayChannelCompany=companyMapper.payChannel(payCompanyInfo.getCompanyId());
                StringBuffer buffer = new StringBuffer(strPayChannelCompany);
                buffer.replace(3, 4, "1");
                payChannelCompany=buffer.toString();
            }else if (payCompanyInfo.getSuiXingPay()!=null){
                payChannel=14;
                payCompanyInfo.setPayChannel(payChannel);

                json = JSON.toJSONString(payCompanyInfo.getSuiXingPay());
                String strPayChannelCompany=companyMapper.payChannel(payCompanyInfo.getCompanyId());
                StringBuffer buffer = new StringBuffer(strPayChannelCompany);
                buffer.replace(13, 14, "1");
                payChannelCompany=buffer.toString();
            }else if (payCompanyInfo.getPsbc()!=null){
                payChannel=23;
                payCompanyInfo.setPayChannel(payChannel);

                json = JSON.toJSONString(payCompanyInfo.getPsbc());
                String strPayChannelCompany=companyMapper.payChannel(payCompanyInfo.getCompanyId());
                StringBuffer buffer = new StringBuffer(strPayChannelCompany);
                buffer.replace(22, 23, "1");
                payChannelCompany=buffer.toString();
            }
            Company company=new Company();
            company.setCompanyId(payCompanyInfo.getCompanyId());
            company.setPayChannel(payChannelCompany);
            Result result=new Result();
            int udpdateCompany=companyMapper.updateByPrimaryKeySelective(company);
            if (udpdateCompany<=0){
                result.setCode("0001");
                result.setMsg("新增配置信息失败");
                return result;
            }
            payCompanyInfo.setPayJson(json);
            try {
                int udpdateCompanyInfo=payCompanyInfoMapper.insertSelective(payCompanyInfo);
                if (udpdateCompanyInfo<=0){
                    result.setCode("0001");
                    result.setMsg("新增配置信息失败");
                    return result;
                }
            }catch (Exception e){
                if(e instanceof DuplicateKeyException){
                    result.setCode("0007");
                    result.setMsg("此通道已经配置了");
                    return result;
                }else {
                    result.setCode("2301");
                    result.setMsg("系统异常");
                    return result;
                }
            }
            result.setCode("0000");
            result.setMsg("成功");
            result.setSuccess(true);
            return result;
        }else {
            Result result=new Result();
            result.setCode("0005");
            result.setMsg("非按商户配置");
            return result;
        }
    }

    @Override
    public Result query(String companyId) {
        PayCompanyInfo payCompanyInfo=new PayCompanyInfo();
        payCompanyInfo.setCompanyId(companyId);
        List<PayCompanyInfo> list= payCompanyInfoMapper.select(payCompanyInfo);
        Result result=new Result();
        if (list.size()<=0){
            result.setCode("0006");
            result.setMsg("查询为空");
            return result;
        }
        result.setCode("0000");
        result.setMsg("成功");
        result.setSuccess(true);
        result.setData(list);
        return result;
    }

    @Override
    public Result revise(PayCompanyInfo payCompanyInfo) {
        Result result=new Result();
        if (payCompanyInfo.getCompanyId()==null || payCompanyInfo.getCompanyId().equals("")){
            result.setCode("0008");
            result.setMsg("修改配置信息的编号和通道为空");
            return result;
        }
//        if (payCompanyInfo.getPayChannel()==null || payCompanyInfo.getPayChannel().equals("")){
//            result.setCode("0008");
//            result.setMsg("修改配置信息的编号和通道为空");
//            return result;
//        }
        //是否需要配置设备的支付json
        Boolean boolIsEverySet=companyMapper.getIsEverySet(payCompanyInfo.getCompanyId());
        if (!boolIsEverySet){
            byte payChannel;
            String json="";
            if (payCompanyInfo.getWxPayJsonVo()!=null){
                payChannel=1;
                payCompanyInfo.setPayChannel(payChannel);
                json = JSON.toJSONString(payCompanyInfo.getWxPayJsonVo());
            }else if (payCompanyInfo.getZfbPayJsonVo()!=null){
                payChannel=2;
                payCompanyInfo.setPayChannel(payChannel);

                json = JSON.toJSONString(payCompanyInfo.getZfbPayJsonVo());
            }else if (payCompanyInfo.getUnionJsonPayVo()!=null){
                payChannel=4;
                payCompanyInfo.setPayChannel(payChannel);

                json = JSON.toJSONString(payCompanyInfo.getUnionJsonPayVo());
            }else if (payCompanyInfo.getSuiXingPay()!=null){
                payChannel=14;
                payCompanyInfo.setPayChannel(payChannel);

                json = JSON.toJSONString(payCompanyInfo.getSuiXingPay());
            }else if (payCompanyInfo.getPsbc()!=null){
                payChannel=23;
                payCompanyInfo.setPayChannel(payChannel);

                json = JSON.toJSONString(payCompanyInfo.getPsbc());
            }

            payCompanyInfo.setPayJson(json);
            Example example = new Example(PayCompanyInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("companyId",payCompanyInfo.getCompanyId());
            int i=mapper.updateByExampleSelective(payCompanyInfo,example);
            if (i<=0){
                result.setCode("0001");
                result.setMsg("修改配置信息失败");
                return result;
            }
            result.setCode("0000");
            result.setMsg("成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setCode("0005");
            result.setMsg("非按商户配置");
            return result;
        }
    }

    @Override
    public Result deletes(Map<String, Object> map) {
        Example example = new Example(PayCompanyInfo.class);
        Example.Criteria criteria = example.createCriteria();
        Result result=new Result();
        if (map.get("companyId")==null || map.get("payChannel")==null){
            result.setCode("0008");
            result.setMsg("删除配置信息的编号和通道为空");
            return result;
        }
        String companyId=String.valueOf(map.get("companyId"));
        int payChannel= (int) map.get("payChannel");
        Company company=new Company();
        String strPayChannelCompany=companyMapper.payChannel(companyId);
        if (payChannel==1){

            StringBuffer buffer = new StringBuffer(strPayChannelCompany);
            buffer.replace(0, 1, "0");
            strPayChannelCompany=buffer.toString();
        }else if (payChannel==2){
            payChannel=2;
            StringBuffer buffer = new StringBuffer(strPayChannelCompany);
            buffer.replace(1, 2, "0");
            strPayChannelCompany=buffer.toString();
        }else if (payChannel==4){
            StringBuffer buffer = new StringBuffer(strPayChannelCompany);
            buffer.replace(3, 4, "0");
            strPayChannelCompany=buffer.toString();
        }else if (payChannel==14){

            StringBuffer buffer = new StringBuffer(strPayChannelCompany);
            buffer.replace(13, 14, "0");
            strPayChannelCompany=buffer.toString();
        }else if (payChannel==23){

            StringBuffer buffer = new StringBuffer(strPayChannelCompany);
            buffer.replace(22, 23, "0");
            strPayChannelCompany=buffer.toString();
        }
        company.setCompanyId(companyId);
        company.setPayChannel(strPayChannelCompany);
        int udpdateCompany=companyMapper.updateByPrimaryKeySelective(company);
        criteria.andEqualTo("companyId",map.get("companyId"));
        criteria.andEqualTo("payChannel",payChannel);
        int i=mapper.deleteByExample(example);
        if (i<=0){
            result.setCode("0001");
            result.setMsg("删除配置信息失败");
            return result;
        }
        result.setCode("0000");
        result.setMsg("删除成功");
        result.setSuccess(true);
        return result;
    }
}
